<template>
  <div>
    <!-- 首页 -->
    <div class="sy" @click="sy">首页</div>
    <!-- 注册框 -->
    <div class="register">
      <el-form
        status-icon
        label-width="0"
        class="form"
        ref="regRef"
        :model="register"
        :rules="rules"
      >
        <el-form-item> </el-form-item>
        <el-form-item prop="username">
          <el-input
            placeholder="用户名"
            v-model.trim="register.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="设置密码"
            v-model.trim="register.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="Spassword">
          <el-input
            placeholder="确认密码"
            v-model.trim="register.Spassword"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="tel">
          <el-input
            placeholder="电话号码"
            v-model.trim="register.tel"
          ></el-input>
        </el-form-item>
        <el-form-item prop="date" default-time="">
          <el-date-picker
            type="date"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            placeholder="出生日期"
            v-model="register.date"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item prop="sex">
          <el-radio label="女" border class="m" v-model="register.sex"
            >我是美女</el-radio
          >
          <el-radio label="男" border class="m" v-model="register.sex"
            >我是帅哥</el-radio
          >
        </el-form-item>
        <el-form-item prop="place">
          <el-input
            type="textarea"
            placeholder="收货地址"
            v-model.trim="register.place"
          ></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btn">
          <el-button type="danger" @click="reg">注册</el-button>
          <el-button type="danger" @click="resetReg">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 文字1-->
    <div class="wz">
      <img src="../assets/img/14.png" alt="" />
    </div>
    <!-- 文字2 -->
    <div class="pb">
      <img src="../assets/img/15.png" alt="" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 验证用户名格式是否正确
    var reg_username = /^[a-zA-Z0-9_-]{4,16}$/;
    var validateUsername = (rule, value, callback) => {
      if (!reg_username.test(value)) {
        callback(new Error("用户名必须是4到16位字母、数字、下划线的组合！"));
      }
      callback();
    };

    // 验证密码格式是否正确
    var reg_password =
      /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
    var validatePsd = (rule, value, callback) => {
      if (!reg_password.test(value)) {
        callback(
          new Error("密码最少6位 要包括大写字母 小写字母 数字 特殊字符！")
        );
      }
      callback();
    };

    // 验证密码的二次输入是否正确
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码！"));
      } else if (value !== this.register.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    //  验证手机号码格式是否正确
    var reg_tel =
      /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    var validateTel = (rule, value, callback) => {
      if (!reg_tel.test(value)) {
        callback(new Error("请正确输入手机号！"));
      }
      callback();
    };

    return {
      // 注册表单的数据绑定对象
      register: {
        username: "",
        password: "",
        tel: "",
        date: "",
        sex: "",
        place: "",
      },
      Spassword: "",
      // 注册表单的验证规则绑定对象
      rules: {
        // 验证用户名是否合法
        username: [
          {
            validator: validateUsername,
            trigger: "blur",
          },
          { required: true },
        ],
        // 验证密码是否合法
        password: [
          {
            validator: validatePsd,
            required: true,
            trigger: "blur",
          },
        ],
        // 验证密码的二次输入是否正确
        Spassword: [{ validator: validatePass, trigger: "blur" }],
        // 验证手机号是否合法
        tel: [
          {
            validator: validateTel,
            required: true,
            trigger: "blur",
          },
        ],
        // 验证出生日期
        date: [
          {
            required: true,
            message: "请选择出生日期！",
            trigger: "change",
          },
        ],
        // 验证性别是否合法
        sex: [{ required: true, message: "请选择性别！", trigger: "change" }],
        // 验证收货地址是否合法
        place: [
          { required: true, message: "请输入收货地址！", trigger: "blur" },
          {
            min: 10,
            message: "长度至少为9个字符！",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    // 首页
    sy() {
      this.$router.push("/home2");
    },
    // 点击重置按钮重置注册表单 regRef为表单的引用对象  调用表单的resetFields方法重置表单
    resetReg() {
      this.$refs.regRef.resetFields();
    },
    // 点击注册按钮注册表单
    reg() {
      // 对表单进行预验证 调用表单的validate函数对表单进行预验证 参数valid为布尔值
      this.$refs.regRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("/register", this.register);
        if (res.status !== 200)
          return this.$message.error("注册失败！手机号或用户名已被占用！");
        this.$message.success("注册成功！");
        // 注册成功后通过编程式导航跳转到登录主页
        this.$router.push("/login2");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.sy {
  position: absolute;
  cursor: pointer;
  top: 700px;
  left: 50px;
  color: #fff;
  background-color: #b71c1c;
  width: 60px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border-radius: 6px;
  border: 2px solid #b71c1c;
}
.register {
  user-select: none;
  position: absolute;
  z-index: 1;
  top: 90px;
  left: 620px;
  width: 360px;
  height: 560px;
  border-radius: 5px;
  background-color: #fff;
  border: 2px solid #b71c1c;
  .btn {
    display: flex;
    justify-content: center;
  }
  .form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    // 盒子模型
    box-sizing: border-box;
  }
  .m {
    color: #c2c7ce;
  }
}
.wz {
  position: absolute;
  top: -16px;
  left: 0px;
  width: 780px;
  img {
    width: 100%;
  }
}

.pb {
  position: absolute;
  top: 30px;
  right: 150px;
  height: 600px;
  img {
    height: 100%;
  }
}
</style>